﻿function ValiCostCenterCode(ddlObj) {
    var code = ddlObj.value;
    if (code == null || code == "" || code == "ALL") {
        alert("You must select cost center!");
        return false;
    }
    else {
        return true;
    }
}

function Menu(theform, menuid) {
    var MenuClass, MenuLenArr, MenuArr, SubMenuArr, MenuIdArr
    var splitchar1, splitchar2
    var arr, subarr, arrlen

    MenuArr = new Array()
    MenuLenArr = new Array()
    SubMenuArr = new Array()
    MenuIdArr = new Array()

    MenuArr[1] = MenuStr(hfDGs);
    MenuArr[2] = MenuStr(hfDGDs);
    MenuArr[3] = MenuStr(hfDCCGs);
    MenuArr[4] = MenuStr(hfCCGCCs);

    MenuIdArr[1] = ddlDepartmentGroup;
    MenuIdArr[2] = ddlDepartment;
    MenuIdArr[3] = ddlCostCenterGroup;
    MenuIdArr[4] = ddlCostCenter;
    MenuClass = 4


    splitchar1 = "|||";
    splitchar2 = "###";
    for (iii = 1; iii <= MenuClass; iii++) {
        arr = MenuArr[iii].split(splitchar1);
        subarr = new Array()
        for (i = 0; i < arr.length; i++) {
            subarr[i] = arr[i].split(splitchar2);
        }
        SubMenuArr[iii] = subarr
        MenuLenArr[iii] = subarr.length
    }

    var self, submenu, thislen, thisarr
    var subccgmenu, submenucc
    self = eval("document." + theform.name + "." + MenuIdArr[menuid]);
    submenu = eval("document." + theform.name + "." + MenuIdArr[menuid + 1]);
    subccgmenu = eval("document." + theform.name + "." + MenuIdArr[menuid + 2]);
    submenucc = eval("document." + theform.name + "." + MenuIdArr[menuid + 3]);
    var submenuindex = submenu.value;
    var submenuccgindex = subccgmenu.value;
    var submenuccindex = submenucc.value;


    thislen = MenuLenArr[menuid + 1]
    thisarr = SubMenuArr[menuid + 1]
    submenu.length = 0
    submenu.options.add(new Option("ALL", "ALL"));
    if (self.value.indexOf("ALL") >= 0) {
        for (i = 0; i < thislen; i++) {
            for (j = 0; j < self.options.length; j++) {
                if (thisarr[i][0] == self.options[j].value) {
                    submenu.options.add(new Option(thisarr[i][1], thisarr[i][1]));
                }
            }
        }
    }
    else {
        for (i = 0; i < thislen; i++) {
            if (thisarr[i][0] == self.value) {
                submenu.options.add(new Option(thisarr[i][1], thisarr[i][1]));
            }
        }
    }

    thisccglent = MenuLenArr[menuid + 2];
    thisccgarr = SubMenuArr[menuid + 2];
    subccgmenu.length = 0;
    subccgmenu.options.add(new Option("ALL", "ALL"));
    for (i = 0; i < thisccglent; i++) {
        for (j = 0; j < submenu.options.length; j++) {
            if (thisccgarr[i][0] == submenu.options[j].value) {
                subccgmenu.options.add(new Option(thisccgarr[i][1], thisccgarr[i][1]));
            }
        }
    }

    thiscclength = MenuLenArr[menuid + 3];
    thisccarr = SubMenuArr[menuid + 3];
    submenucc.length = 0;
    //submenucc.options.add(new Option("ALL", "ALL"));
    for (i = 0; i < thiscclength; i++) {
        for (j = 0; j < subccgmenu.options.length; j++) {
            if (thisccarr[i][0] == subccgmenu.options[j].value) {
                submenucc.options.add(new Option(thisccarr[i][1], thisccarr[i][1]));
            }
        }
    }
}

function MenuDepartment(theform, menuid) {
    var MenuClass, MenuLenArr, MenuArr, SubMenuArr, MenuIdArr
    var splitchar1, splitchar2
    var arr, subarr, arrlen

    MenuArr = new Array()
    MenuLenArr = new Array()
    SubMenuArr = new Array()
    MenuIdArr = new Array()

    MenuArr[1] = MenuStr(hfDGs);
    MenuArr[2] = MenuStr(hfDGDs);
    MenuArr[3] = MenuStr(hfDCCGs);
    MenuArr[4] = MenuStr(hfCCGCCs);

    MenuIdArr[1] = ddlDepartmentGroup;
    MenuIdArr[2] = ddlDepartment;
    MenuIdArr[3] = ddlCostCenterGroup;
    MenuIdArr[4] = ddlCostCenter;
    MenuClass = 4


    splitchar1 = "|||";
    splitchar2 = "###";
    for (iii = 1; iii <= MenuClass; iii++) {
        arr = MenuArr[iii].split(splitchar1);
        subarr = new Array()
        for (i = 0; i < arr.length; i++) {
            subarr[i] = arr[i].split(splitchar2);
        }
        SubMenuArr[iii] = subarr
        MenuLenArr[iii] = subarr.length
    }

    var self, submenu, thislen, thisarr
    var subccgmenu, submenucc
    self = eval("document." + theform.name + "." + MenuIdArr[menuid]);
    submenu = eval("document." + theform.name + "." + MenuIdArr[menuid + 1]);
    submenucc = eval("document." + theform.name + "." + MenuIdArr[menuid + 2]);

    var submenuindex = submenu.value;
    var submenuccindex = submenucc.value;

    thislen = MenuLenArr[menuid + 1]
    thisarr = SubMenuArr[menuid + 1]
    submenu.length = 0
    submenu.options.add(new Option("ALL", "ALL"));
    if (self.value.indexOf("ALL") >= 0) {
        for (i = 0; i < thislen; i++) {
            for (j = 0; j < self.options.length; j++) {
                if (thisarr[i][0] == self.options[j].value) {
                    submenu.options.add(new Option(thisarr[i][1], thisarr[i][1]));
                }
            }
        }
    }
    else {
        for (i = 0; i < thislen; i++) {
            if (thisarr[i][0] == self.value) {
                submenu.options.add(new Option(thisarr[i][1], thisarr[i][1]));
            }
        }
    }

    thiscclength = MenuLenArr[menuid + 2];
    thisccarr = SubMenuArr[menuid + 2];
    submenucc.length = 0;
    //submenucc.options.add(new Option("ALL", "ALL"));
    for (i = 0; i < thiscclength; i++) {
        for (j = 0; j < submenu.options.length; j++) {
            if (thisccarr[i][0] == submenu.options[j].value) {
                submenucc.options.add(new Option(thisccarr[i][1], thisccarr[i][1]));
            }
        }
    }
}


function MenuCostCenterGroup(theform, menuid) {
    var MenuClass, MenuLenArr, MenuArr, SubMenuArr, MenuIdArr
    var splitchar1, splitchar2
    var arr, subarr, arrlen

    MenuArr = new Array()
    MenuLenArr = new Array()
    SubMenuArr = new Array()
    MenuIdArr = new Array()

    MenuArr[1] = MenuStr(hfDGs);
    MenuArr[2] = MenuStr(hfDGDs);
    MenuArr[3] = MenuStr(hfDCCGs);
    MenuArr[4] = MenuStr(hfCCGCCs);

    MenuIdArr[1] = ddlDepartmentGroup;
    MenuIdArr[2] = ddlDepartment;
    MenuIdArr[3] = ddlCostCenterGroup;
    MenuIdArr[4] = ddlCostCenter;
    MenuClass = 4


    splitchar1 = "|||";
    splitchar2 = "###";
    for (iii = 1; iii <= MenuClass; iii++) {
        arr = MenuArr[iii].split(splitchar1);
        subarr = new Array()
        for (i = 0; i < arr.length; i++) {
            subarr[i] = arr[i].split(splitchar2);
        }
        SubMenuArr[iii] = subarr
        MenuLenArr[iii] = subarr.length
    }

    var self, submenu, thislen, thisarr
    var subccgmenu, submenucc
    self = eval("document." + theform.name + "." + MenuIdArr[menuid]);
    submenu = eval("document." + theform.name + "." + MenuIdArr[menuid + 1]);

    var submenuindex = submenu.value;

    thislen = MenuLenArr[menuid + 1]
    thisarr = SubMenuArr[menuid + 1]
    submenu.length = 0;
    //submenu.options.add(new Option("ALL", "ALL"));
    if (self.value.indexOf("ALL") >= 0) {
        for (i = 0; i < thislen; i++) {
            for (j = 0; j < self.options.length; j++) {
                if (thisarr[i][0] == self.options[j].value) {
                    submenu.options.add(new Option(thisarr[i][1], thisarr[i][1]));
                }
            }
        }
    }
    else {
        for (i = 0; i < thislen; i++) {
            if (thisarr[i][0] == self.value) {
                submenu.options.add(new Option(thisarr[i][1], thisarr[i][1]));
            }
        }
    }
}

function MenuStr(id) {
    return document.getElementById(id).value;
}

function SelectObj(theform, menuid) {
    this.theForm = theform;
    this.menuID = menuid;

    var arr, subarr, arrlen

    var MenuClass = 4
    var splitchar1 = "|||";
    var splitchar2 = "###";

    this.MenuArr = new Array()
    MenuArr[1] = MenuStr(hfDGs);
    MenuArr[2] = MenuStr(hfDGDs);
    MenuArr[3] = MenuStr(hfDCCGs);
    MenuArr[4] = MenuStr(hfCCGCCs);

    this.MenuIdArr = new Array()
    MenuIdArr[1] = ddlDepartmentGroup;
    MenuIdArr[2] = ddlDepartment;
    MenuIdArr[3] = ddlCostCenterGroup;
    MenuIdArr[4] = ddlCostCenter;

    this.MenuLenArr = new Array()
    this.SubMenuArr = new Array()

    for (iii = 1; iii <= MenuClass; iii++) {
        arr = MenuArr[iii].split(splitchar1);
        subarr = new Array();
        for (i = 0; i < arr.length; i++) {
            subarr[i] = arr[i].split(splitchar2);
        }
        SubMenuArr[iii] = subarr
        MenuLenArr[iii] = subarr.length
    }

    this.self = eval("document." + theform.name + "." + MenuIdArr[this.menuID]);
}
SelectObj.prototype.SelectFirstSubObj = function() {
    var submenu = eval("document." + theform.name + "." + MenuIdArr[this.menuID + 1]);

    var submenuindex = submenu.value;

    var thislen = MenuLenArr[this.menuID + 1]
    var thisarr = SubMenuArr[this.menuID + 1]
    submenu.length = 0;
    submenu.options.add(new Option("ALL", "ALL"));
    if (this.self.value.indexOf("ALL") >= 0) {
        for (i = 0; i < thislen; i++) {
            for (j = 0; j < this.self.options.length; j++) {
                if (thisarr[i][0] == this.self.options[j].value) {
                    submenu.options.add(new Option(thisarr[i][1], thisarr[i][1]));
                }
            }
        }
    }
    else {
        for (i = 0; i < thislen; i++) {
            if (thisarr[i][0] == this.self.value) {
                submenu.options.add(new Option(thisarr[i][1], thisarr[i][1]));
            }
        }
    }
}
SelectObj.prototype.SelectOtherObj = function() {
    var loopNum;
    if (this.menuID == 1) {
        loopNum = 2;
    }
    else if (this.menuID == 2) {
        loopNum = 1;
    }
    else if (this.menuID == 3) {
        loopNum = 0;
    }
    else if (this.menuID == 4) {
        loopNum = -1;
    }
    for (var m = 1; m <= loopNum; m++) {
        var menu = eval("document." + theform.name + "." + MenuIdArr[this.menuID + m]);

        thislength = this.MenuLenArr[this.menuID + m];
        thisarr = this.SubMenuArr[this.menuID + m];
        submenu.length = 0;
        submenu.options.add(new Option("ALL", "ALL"));
        for (i = 0; i < thislength; i++) {
            for (j = 0; j < menu.options.length; j++) {
                if (thisccarr[i][0] == menu.options[j].value) {
                    submenu.options.add(new Option(thisccarr[i][1], thisccarr[i][1]));
                }
            }
        }
    }
}

function SelectChangeObjs(theform, menuid) {
    var selectObj = SelectObj(theform, menuid);
    selectObj.SelectFirstSubObj();
    selectObj.SelectOtherObj();
}


function GetDDLValues(id) {
    this.ddlID = id;
}
GetDDLValues.prototype.GetItems = function() {
    var obj = document.getElementById(this.ddlID);
    if (obj == null) {
        return;
    }
    return obj.value.split("@@@");
}
function GetDepartments() {
    var getDDL = new GetDDLValues(hfDepartmentGroupDepartment);
    return getDDL.GetItems();
}
function GetCostCenterGroups() {
    var getDDL = new GetDDLValues(hfDepartmentCostCenterGroup);
    return getDDL.GetItems();
}
function GetCostCenters() {
    var getDDL = new GetDDLValues(hfGroupCostCenter);
    return getDDL.GetItems();
}



